package io.mft.as.ar.gateway.client;

import java.util.Map;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;

import cn.hutool.json.JSONUtil;
import io.mft.as.ar.gateway.config.BusIdConfig;
import io.mft.as.ar.gateway.exception.GatewayClientException;
import io.mft.as.ar.gateway.factory.BusClientFactory;
import io.mft.as.ar.gateway.result.Result;
import io.mft.commons.config.ConfigProvider;
import io.mft.dev.base.cat.CatContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 描述: 网关客户端调用类
 * 版权: Copyright (c) 2013 
 * 公司: 思迪科技 
 * 作者: 李建 
 * 版本: 1.0 
 * 创建日期: 2013-1-21 
 * 创建时间: 下午4:31:29
 */
@Slf4j
public class Client
{
    
    
    private String        serverName = "";
    
    private int           branch     = 0;
    
    public Client()
    {
        this.serverName = BusIdConfig.getDefaultSystemId();
    }
    
    public Client(String serverName)
    {
        this.serverName = serverName;
    }
    
    public Client(String serverName, int branch)
    {
        this.serverName = serverName;
        this.branch = branch;
    }
    
    /**
     * 
     * 设置分支号 
     */
    public void setBranch(int branch)
    {
        this.branch = branch;
    }
    
    /**
     * 描述：网关调用
     * 作者：李建
     * 时间：2013-3-30 下午5:48:22
     * @param param
     * @return 返回Result类型数据
     */
    public Result getResult(int funcNo, Map param)
    {
        Result result = invokeToResult(funcNo, param);
        return result;
    }
    
    /**
     * 描述：新增加Result类型返回值
     * 作者：李建
     * 时间：2013-3-30 下午5:38:52
     * @param paraMap
     * @return
     * @throws Exception
     */
    public Result invokeToResult(int funcNo, Map paraMap)
    {
        Result result = new Result();
        int use= ConfigProvider.get("system.cat.use", 1);
        Transaction t= null;
        if (use==1) {
			String name=serverName+"-"+String.valueOf(funcNo);
        	//t= Cat.newTransaction("bus", name);
		}
        try
        {
        	/**
        	if (use==1) {
            	CatContext catContext=CatContext.instanceThreadCatContext();
            	Cat.logRemoteCallServer(catContext);
    			paraMap.put(Cat.Context.ROOT, catContext.getProperty(Cat.Context.ROOT));
    			paraMap.put(Cat.Context.PARENT, catContext.getProperty(Cat.Context.PARENT));
    			paraMap.put(Cat.Context.CHILD, catContext.getProperty(Cat.Context.CHILD));
                Cat.logEvent("bus.function", String.valueOf(funcNo), Message.SUCCESS, JSONUtil.toJsonStr(paraMap));
			}**/
            BusClient tbClient = BusClientFactory.getClient(serverName);
            if ( branch > 0 )//设置分支号
            {
                tbClient.setBranch(branch);
            }
            result = tbClient.invoke(funcNo, paraMap);
            /**
            if (use==1) {
                Cat.logEvent("bus.result", String.valueOf(funcNo), Message.SUCCESS, JSONUtil.toJsonStr(result.dataMap()));
			}
    		**/
        }
        catch (GatewayClientException ex)
        {
            log.error(ex.getMessage(), ex);
            result.setErr_no(ex.getErrorNo());
            result.setErr_info(ex.getMessage());
            if (use==1) {
            	 //Cat.logEvent("bus.result", String.valueOf(funcNo), Message.SUCCESS, JSONUtil.toJsonStr(result.dataMap()));
			}
           
        }
        catch (Exception ex)
        {
            log.error(ex.getMessage(), ex);
            result.setErr_no( -1);
            result.setErr_info(ex.getMessage());
            if (use==1) {
           	  // Cat.logEvent("bus.result", String.valueOf(funcNo), Message.SUCCESS, JSONUtil.toJsonStr(result.dataMap()));
			}
        }finally {
            if (use==1) {
            	if (t!=null) {
            		//t.complete();
				}
			}
		}
        return result;
    }
}
